package com.dap.core.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class IOUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(IOUtils.class);


    public static String stream2string(InputStream is) throws IOException {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line = null;
            boolean firstLine = true;
            while ((line = reader.readLine()) != null) {
                if (!firstLine) {
                    sb.append(System.getProperty("line.separator"));
                } else {
                    firstLine = false;
                }
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            try {
                is.close();
            } catch (Exception e) {
                LOGGER.error("Resource close exception", e);
            }
        }
    }


    public static Properties stream2props(InputStream is) throws IOException {
        Properties props = new Properties();
        try {
            props.load(is);

            return props;
        } catch (IOException ioe) {
            throw ioe;
        } finally {
            try {
                is.close();
            } catch (Exception e) {
                LOGGER.error("Resource close exception", e);
            }
        }
    }
}
